#Binary parser template

import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class YS_Origin_YMO(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(YS_Origin_YMO,self).__init__("YMO")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_vertices(self, meshName, numVerts, vertSize):
        
        print "%d vertices" %numVerts
        for i in xrange(numVerts):
            vx, vy, vz = self.inFile.read_float(3)
            nx, ny, nz = self.inFile.read_float(3)
            self.inFile.read_long(2)
            tu, tv = self.inFile.read_float(2)
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            self.add_vert_uv(meshName, i, [tu, tv])
            
    def parse_faces(self, meshName, numFaces):
        
        print "%d faces" %numFaces
        for i in xrange(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        idstring = self.inFile.read_string(4)
        print self.inFile.read_long(8)
        self.inFile.seek(40, 1)
        
        for i in xrange(3):
            print self.inFile.read_string(192, "\x00")
            
        self.inFile.read_long()
        self.inFile.seek(72, 1)
        
        meshName = self.inFile.read_string(96, "\x00")
        texName = self.inFile.read_string(576, "\x00")
        if not meshName: 
            meshName = "temp"
        self.create_object(meshName)
        
        #FRot 1
        self.inFile.seek(96, 1)
        self.inFile.seek(96, 1)
        self.inFile.seek(288, 1)
        self.inFile.seek(192, 1) #floats
        self.inFile.seek(288, 1)
        
        #FRot 2
        self.inFile.seek(96, 1)
        self.inFile.seek(96, 1)
        self.inFile.seek(192, 1)
        #self.inFile.seek(480, 1) #animation        
        #self.inFile.seek(192, 1)
        
        #mesh
        self.inFile.seek(96, 1)
        self.inFile.seek(40, 1)
        self.inFile.seek(84, 1)
        
        numVerts, vertSize, unk3 = self.inFile.read_long(3)
        self.parse_vertices(meshName, numVerts, vertSize)
        null, null, numIndex, unk4, null = self.inFile.read_long(5)
        self.parse_faces(meshName, numIndex / 3)
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = YS_Origin_YMO(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "YMO", "YMO", ""

if __name__ == '__main__':
    
    file1 = ""
    obj = read_file(file1)
    